package com.authenticateCenter.factory;


import com.authenticateCenter.domain.dto.UserDto;
import com.authenticateCenter.domain.entity.User;
import com.authenticateCenter.domain.vo.UserVo;
import com.security.domain.authority.CommonUserDetails;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author 大忽悠
 * @create 2022/10/8 21:20
 */
public class UserFactory {
    public static UserConverter USER_CONVERT = Mappers.getMapper(UserConverter.class);

    @Mapper(uses = {CustomMapper.class, RoleFactory.RoleConverter.class})
    public interface UserConverter {
        @Mapping(target = "id", ignore = true)
        User toUser(UserDto userDto);

        @Mapping(target = "roles", ignore = true)
        UserVo toUserVo(User user);

        @Mapping(target = "roles", ignore = true)
        List<UserVo> toUserVoList(List<User> user);

        @Mapping(source = "roles",target = "commonRoles")
        CommonUserDetails toUserDetails(UserVo userVo);
    }

    public static class CustomMapper {
        public Integer map(Boolean value) {
            if (value!=null&&value) {
                return 1;
            }
            return 0;
        }

        public Boolean map(Integer value){
            if(value!=null&&value>=1){
                return Boolean.TRUE;
            }
            return Boolean.FALSE;
        }
    }
}
